<template>
  <div class="com-update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '修改中'
      })
      const formatDate = dayjs(this.currentDate).format('YYYY-MM-DD')
      await updateUserProfile({
        birthday: formatDate
      })
      this.$emit('input', formatDate)
      this.$toast.success('修改成功')
      this.$emit('close')
    }
  }
}
</script>

<style lang="less" scoped>
.com-update-name {
  height: 100%;
  background-color: #f5f7f9;
  .input-wrap {
    padding: 10px;
  }
}
</style>
